package dir;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author wzby
 * @Date: 2022/8/1 16:58
 */
public class Dir6 {
    static long allrows = 0;
    static int count = 0;
    public static void main(String[] args) {
       /* String path = "";
        File f = new File(path);
        System.out.println(f.getAbsoluteFile());
        System.out.println(System.getProperty("user.dir"));*/

        //统计写了多少个java文件
        File f1 = new File("E:\\idea\\java");
        count(f1);
        System.out.println(count);
        System.out.printf("有%d个Java文件，共有%d行代码",count,allrows);
    }
        //创建count方法
    private static void count(File dir) {
        if (dir.isDirectory()){
            File[] fs  = dir.listFiles();
            for (File t : fs){
                if (t.isDirectory()){
                    count(t);
                }else if (t.isFile() && t.getName().endsWith(".java")){
                    ++count;
                    try (var fis = new FileInputStream(t)){
                        String src = new String (fis.readAllBytes());
                        long rows = src.lines().count();
                        allrows += rows;
                        System.out.printf("%s(%d行)%n",t.getAbsoluteFile(),rows);
                    }catch (Exception e){

                    }
                }
            }
        }
    }
}
